<?php
App::uses('Component', 'Controller');
class SudokuComponent extends Component {
	
	//check has duplicate in row
	private function hasDuplicate($array){
		for($i=0; $i<count($array) - 1; $i++){
			for($j=$i+1; $j<count($array); $j++){
				if($array[$i] == $array[$j]){
					return true;
				}
			}
		}
		return false;
	}
	
	//check has not integer param
	private function hasNotIntergerParam($array){
		foreach ($array as $item){
			if(!is_int($item)){
				return true;
			}
		}
		return false;
	}
	
	private function hasNumberLessThanZero($listGuess){
		foreach ($listGuess as $item){
			if($item < 0){
				return true;
			}
		}
		return false;
	}
	
	private function hasNotIntegerParam2DArray($array){
		for($i=0;$i<count($array[0]); $i++){
			if($this->hasNotIntergerParam($array[$i])){
				return true;
			}
		}
		return false;
	}
	
	private function hasDuplicateRow($array){
		for($i=0; $i<count($array[0]); $i++){
			if($this->hasDuplicate($array[$i])){
				return true;
			}
		}
		return false;
	}
	
	private function hasDuplicateColumn($array){
		for($i=0; $i<count($array[0]); $i++){
			for($j=0; $j<count($array[0]) -1; $j++){
				for($g=$j+1; $g<count($array[0]); $g++){
					if($array[$i][$j] == $array[$i][$g]){
						return true;
					}
				}
			}
		}
		return false;
	}
	
	private function hasRowNot1To9($array){
		for($i=0; $i<count($array[0]); $i++){
			for($j=0; $j<count($array[0]); $j++){
				if($array[$i][$j] < 1 || $array[$i][$j] > 9){
					return true;
				}
			}
		}
		return false;
	}
	
	private function hasColNot1To9($array){
		for($i=0; $i<count($array[0]); $i++){ //col
			for($j=0; $j<count($array[0]); $j++){ //row
				if($array[$j][$i] < 1 || $array[$j][$i] > 9){
					return true;
				}
			}
		}
		return false;
	}
	
	private function getArray3x3($array, $fromRow, $fromCol){
		$result = array();
		for($i=$fromRow; $i<$fromRow+3; $i++){
			for($j=$fromCol; $j<$fromCol+3; $j++){
				array_push($result, $array[$i][$j]);
			}
		}
		return $result;
	}
	
	public function doCheckSudoku9x9($array){
		//TODO
		//Check input
		if($this->hasNotIntegerParam2DArray($array)){
			return "has param not integer";
		}
		 
		//Check row
		//Check duplicate
		
	
		if($this->hasDuplicateRow($array)){
			return "has duplicate in row";
		}
	
		if($this->hasRowNot1To9($array)){
			return "has row not 1-9";
		}
	
		 
		//Check column
		//Check conflict
		if($this->hasDuplicateColumn($array)){
			return "has duplicate in column";
		}
	
	
		if($this->hasColNot1To9($array)){
			return "has column not 1-9";
		}
	
		$listChildArray = array();
	
		for($i=0; $i<count($array[0])-1; $i = $i+3){
			for($j=0; $j<count($array[0])-1; $j = $j+3){
				array_push($listChildArray, $this->getArray3x3($array, $i, $j));
			}
		}
	
	
		foreach ($listChildArray as $child){
				
			if($this->hasDuplicate($child)){
				return "array 3x3 has duplicate number";
			}
				
			foreach ($child as $item){
				if($item < 1 || $item > 9){
					return "array 3x3 not 1-9";
				}
			}
		}
	
		return "is sudoku 9x9";
	}
}